/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.home.core;

import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Campaign extends Invitations {
	@SuppressWarnings("unused")
	private double lat, lon, radius;
	Vector<Campaign> campaigns = new Vector<Campaign>();
	//private Vector<String> users;
		
	public String[] getActiveUsers() { return null; } //TODO
	public static Campaign[] fromXml(String xml) { return null; } //TODO
	
	public void update(String xml) { };//TODO

	public static Campaign[] fromDataBase() { return null; } //TODO
	
	public Vector<Campaign> fromXml(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new Campaign.MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml); 
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return campaigns;
	}
	
	public long getId() {
		return this.id;
	}
	
	class MyHandler extends DefaultHandler {
		String lastElement;
		boolean begin;
		boolean valid;
		
		int count = 0;

		public void endElement(String uri, String localName, String qName) {
			begin = false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin = true;
			if (localName.compareTo("Campaign") == 0) {
				count++;
				campaigns.add(new Campaign());
				valid = true;
			}
			lastElement = localName;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
			}
		}

		public void characters(char[] ch, int start, int length) {
			if(valid) {
				String s = new String(ch, start, length);
				if (lastElement == null) return;
				if (!begin) return;
				if (lastElement.compareTo("idCampaign") == 0) {
					if (s.compareTo("null") != 0) {
						Campaign camp = campaigns.get(count-1);
						camp.id = Integer.parseInt(s);
					}
				}
			}
		}
	}
}
